import axios from 'axios'
import * as qs from 'qs'

const prefix =
  window.location.href.indexOf('192.168.10.') > -1
    ? '/projectName'
    : 'http://web.gcs360.com/projectName'

async function apiPost(url, data) {
  let result = null
  try {
    await axios
      .post(prefix + url, qs.stringify(data), { withCredentials: true })
      .then(res => {
        console.log('http raw', res)
        result = res
      })
  } catch (err) {
    let message = '出问题了'
    let errString = err.toString()
    let code = errString.substring(errString.lastIndexOf(' ') + 1)
    switch (code) {
      case '404':
        message = '访问地址不存在'
        break
      case '500':
        message = '后台出错'
        break
    }
    window._$vm.$message.error(message)
  }
  return (await result) ? result.data : null
}

function successs(text) {
  window._$vm.$message.success(text)
}

function error(text) {
  window._$vm.$message.error(text)
}

function warning(text) {
  window._$vm.$message.warning(message)
}


export default ({
  apiPost: apiPost
})